package com.qsign.sfrz_android.base;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.qsign.sfrz_android.activity.login.Model.MyUserData;

/**
 * Created by wgy on 17/5/11.
 */
public class YcspUserDataShared {

    private static  final String FILE_NAME="cbb_data";
    private static SharedPreferences mySharedPreference;
    private static YcspUserDataShared instance;
    SharedPreferences.Editor editor;
//    Context context;
    private YcspUserDataShared(){
        mySharedPreference= MyApplication.getInstance().getSharedPreferences(FILE_NAME, MyApplication.getInstance().MODE_PRIVATE);
//        PreferenceManager
//                .getDefaultSharedPreferences(MyApplication.getAppContext());
        editor=mySharedPreference.edit();
    }

    public static synchronized YcspUserDataShared getInstance(Context context) {
        if (instance == null) {
            instance = new YcspUserDataShared();
        }
        return instance;
    }

    public void SaveUserBean(MyUserData userdata){
        Gson gs = new Gson();
        String userstring = gs.toJson(userdata);//把对象转为JSON格式的字符串
        editor.putString("userstr",userstring);
        editor.commit();
    }
    public MyUserData getUserdata(){
        Gson gson = new Gson();
        String userstr=mySharedPreference.getString("userstr", "");
        MyUserData userBean = gson.fromJson(userstr, MyUserData.class);
        return userBean;
    }



    public void clearData() {
        editor.clear().commit();
    }


}
